<?php

/**
 * 国际快递成本
 */
class Application_Model_ExpressCost extends Application_Model_DomainObject{
    
    protected $_id;
    
    protected $_agentCompanyId;//代理公司ID
    
    protected $_expressPriceId;//价格对应ID
    
    protected $_expressId;//快递ID
    
    protected $_cityId;//城市ID

    protected $_firstPrice;//首重价
    
    protected $_subsequentPrice;//续重价
    
    protected $_weight; //重量
    
    protected $_price; //重量对应的价格

    protected $_dateUpdate; //价格最后更新时间
    
    protected $_type; //价格计算方法，0为首重续重计算法，1为价格对应计算法，2单价计算法

    protected $_isDisable;//是否禁用
    
    protected $_channelId;//通道ID
    
    protected $_sortKey;


    /**
     *  价格对应计算法，表示是指标查本，从Price里面拿出价格就是该我重量的价格
     */
    const TYPE_PriceMap = 1;
    
    /**
     * 首重续重计算法 ，表示用首重，续重来计算
     */
    const TYPE_FirstPrice =0;
    
    /**
     * 单价计算法 ，表示价格为每KG为多少钱
     */
    const TYPE_UnitPrice = 2;

    public function getTypeName(){
        $names = array(Application_Model_ExpressCost::TYPE_FirstPrice=>'首重续重计算法'
                ,  Application_Model_ExpressCost::TYPE_PriceMap=>'价格对应计算法'
                ,  Application_Model_ExpressCost::TYPE_UnitPrice=>'单价计算法');
        return $names[$this->_type];
    }

        /**
     * 返回指定价格ID的所有渠道的成本
     * @param type $expressPriceId 
     */
    public static function getByExpress($expressPriceId,$weight){
        $costItems = Application_Model_ExpressCost::getDao()->queryAll(array('expressPriceId'=>$expressPriceId),'weight');
        return Application_Model_ExpressCost::getCultCost($costItems,$weight);
    }
    
    /**
     * 返回最合适的价格计算项
     * @param array $costItems
     * @return array 
     */
    private static function getCultCost($costItems,$weight){
        $map = array();
        foreach ($costItems as $item){
            $key = $item->AgentCompanyId .'_'.$item->ChannelId;
            if(!array_key_exists($key, $map)){
                $map[$key] = $item;
                continue;
            }
            $old = $map[$key];
            if($item->Weight < $weight && $item->Weight > $old->Weight){
                $map[$key] = $item;
            }
        }
        $result = array();
        foreach ($map as $key=>$item){
            $item->SortKey = $item->getMoney($weight);
            $result[] = $item;
        }
        return Uuch_Util::quickSort($result, 'SortKey');        
    }

    /**
     * 根据城市ID和快递方式返回所有渠道的成本
     * @param type $expressId
     * @param type $cityId
     * @param type $weight
     * @return type 
     */
    public static function getByExpressAndCity($expressId,$cityId,$weight){
        $costItems = Application_Model_ExpressCost::getDao()->queryAll(array('expressId'=>$expressId,'cityId'=>$cityId),'weight');
        return Application_Model_ExpressCost::getCultCost($costItems, $weight);
    }
    
    public static function getByCity($cityId,$weight){
        $costItems = Application_Model_ExpressCost::getDao()->queryAll(array('cityId'=>$cityId),'weight');
        return Application_Model_ExpressCost::getCultCost($costItems, $weight);        
    }

    /**
     * 返回指定重量的价格
     * @param int $weight 重量
     * @return real 价格
     */
    public function getMoney($weight){
        if($this->_type == Application_Model_ExpressCost::TYPE_FirstPrice){
            return $this->getMoneyByFirst($weight);
        }else if($this->_type == Application_Model_ExpressCost::TYPE_PriceMap){
            return $this->_price; //直接价格表，返回对应价
        }else{
            return $this->getMoneyByUnitPirce($weight);
        }
    }
    
    /**
     * 根据单位价格计算法来计算价格,计算公式为 重量 / 单位重量 * 单价
     * @param int $weight
     * @return number 
     */
    public function getMoneyByUnitPirce($weight){
        $unitWeight = $this->getExpress()->UnitWeight;
        $mod = $weight % $unitWeight;
        $baishu = ($weight - $mod) / $unitWeight;
        $baishu = $baishu + ($mod>0?1:0);
        return $this->_price * $baishu;
    }

    /**
     * 根据首重计算法计算重量,计算公式为 首重价 + (重量-首重) / 续重 * 续重价
     * @param type $weight 重量
     * @return type  价格
     */
    public function getMoneyByFirst($weight){
        $express = $this->getExpress();
        if($express->firstWeight>=$weight){
            return $this->_firstPrice;
        }
        
        $mod = ($weight-($express->firstWeight)) % $express->subsequentWeight;//余数
        $baishu = ($weight - $express->firstWeight - $mod) / $express->subsequentWeight + ($mod>0?1:0); // 倍数
        $menoy = $this->_firstPrice + $baishu *$this->_subsequentPrice;
        return $menoy;        
    }
    
    private $_express;
    /**
     * 返回对应的快递方式
     * @return Application_Model_Express 
     */
    public function getExpress(){
        if($this->_express==NULL){
            $this->_express = Application_Model_Express::load($this->_expressId);
        }
        return $this->_express;
    }
    
    private $_channel;
    /**
     * 返回出货渠道 
     */
    public function getChannel(){
        if($this->_channel==NULL){
            $this->_channel = Application_Model_ExpressChannel::load($this->_channelId);
        }
        return $this->_channel;
    }
    
    private $_agentCompany;
    /**
     * 返回代码公司
     * @return type 
     */
    public function getAgentCompany(){
        if($this->_agentCompany==NULL){
            $this->_agentCompany = Application_Model_AgentCompany::load($this->_agentCompanyId);
        }
        return $this->_agentCompany;
    }
    
}